package com.yxjwzpx.management.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.yxjwzpx.management.domain.MaintenancePlan;
import com.yxjwzpx.management.mapper.MaintainCompanyMapper;
import com.yxjwzpx.management.mapper.MaintenancePlanMapper;
import com.yxjwzpx.management.service.IMaintenancePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 维保计划Service业务层处理
 * 
 * @author yxjwzpx
 * @date 2025-04-09
 */
@Service
public class MaintenancePlanServiceImpl implements IMaintenancePlanService 
{

    @Autowired
    private MaintenancePlanMapper maintenancePlanMapper;

    @Autowired
    private MaintainCompanyMapper maintainCompanyMapper;

    @Override
    public int sumActualCost(Long companyId) {
        return maintenancePlanMapper.sumActualCost(companyId);
    }

    @Override
    public List<Map<String, Object>> getPlanCostList(Long companyId) {
        return maintenancePlanMapper.getPlanCostList(companyId);
    }



    /**
     * 查询维保计划
     * 
     * @param planId 维保计划主键
     * @return 维保计划
     */
    @Override
    public MaintenancePlan selectMaintenancePlanByPlanId(Long planId)
    {
        return maintenancePlanMapper.selectMaintenancePlanByPlanId(planId);
    }

    /**
     * 查询维保计划列表
     * 
     * @param maintenancePlan 维保计划
     * @return 维保计划
     */
    @Override
    public List<MaintenancePlan> selectMaintenancePlanList(MaintenancePlan maintenancePlan)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();

        if("COMPANY".equals(roles.get(0).getRoleKey())){
            Long companyId = maintainCompanyMapper.selectCompanyIdByUserId(user.getUserId());
            if(companyId!=null){
                maintenancePlan.setCompanyId(companyId);
            }else {
                return new ArrayList<>();
            }
        }
        return maintenancePlanMapper.selectMaintenancePlanList(maintenancePlan);
    }

    /**
     * 新增维保计划
     * 
     * @param maintenancePlan 维保计划
     * @return 结果
     */
    @Override
    public int insertMaintenancePlan(MaintenancePlan maintenancePlan)
    {
        maintenancePlan.setCreateTime(DateUtils.getNowDate());
        return maintenancePlanMapper.insertMaintenancePlan(maintenancePlan);
    }

    /**
     * 修改维保计划
     * 
     * @param maintenancePlan 维保计划
     * @return 结果
     */
    @Override
    public int updateMaintenancePlan(MaintenancePlan maintenancePlan)
    {
        maintenancePlan.setUpdateTime(DateUtils.getNowDate());
        return maintenancePlanMapper.updateMaintenancePlan(maintenancePlan);
    }

    /**
     * 批量删除维保计划
     * 
     * @param planIds 需要删除的维保计划主键
     * @return 结果
     */
    @Override
    public int deleteMaintenancePlanByPlanIds(Long[] planIds)
    {
        return maintenancePlanMapper.deleteMaintenancePlanByPlanIds(planIds);
    }

    /**
     * 删除维保计划信息
     * 
     * @param planId 维保计划主键
     * @return 结果
     */
    @Override
    public int deleteMaintenancePlanByPlanId(Long planId)
    {
        return maintenancePlanMapper.deleteMaintenancePlanByPlanId(planId);
    }
}
